#include <stdio.h>
#include "showbmp.h"
#include "touch2.h"
#include <stdlib.h>
#include <string.h>

int T[9] ; //步数位置记录，避免重复
int T_time = 0;  //全局步数0--8

extern int x_d, y_d;           //触摸屏坐标
int bmp_x[3] = {56, 330, 611}; // 9区域内图片显示坐标
int bmp_y[3] = {12, 180, 348}; // 9区域内图片显示坐标
int t_x[2] = {341, 682};       //触摸屏分为9区域
int t_y[2] = {200, 400};       //触摸屏分为9区域

int x_point, y_point; //图片位置，0--9区域
int area, r_area;     //象限
int v = 0;            //玩家
int one[9], two[9];   //两位玩家的步位置



int show(int x, int y, int type)
{
    if (type % 2 == 0)
    {
        show_bmp("./zj/game/t.bmp", bmp_x[x], bmp_y[y]);
    }
    if (type % 2 == 1)
    {
        show_bmp("./zj/game/x.bmp", bmp_x[x], bmp_y[y]);
    }
    type++;
    if (type == 9)
    {
        type = 0;
    }
    return type;
}

int get_area(int a, int b)
{
    int i_area;
    // 00 10 20
    // 01 11 21
    // 02 12 22
    if (a == 0 && b == 0)
    {
        i_area = 0;
    }
    if (a == 1 && b == 0)
    {
        i_area = 1;
    }
    if (a == 2 && b == 0)
    {
        i_area = 2;
    }
    //-------------//
    if (a == 0 && b == 1)
    {
        i_area = 3;
    }
    if (a == 1 && b == 1)
    {
        i_area = 4;
    }
    if (a == 2 && b == 1)
    {
        i_area = 5;
    }
    //-------------//
    if (a == 0 && b == 2)
    {
        i_area = 6;
    }
    if (a == 1 && b == 2)
    {
        i_area = 7;
    }
    if (a == 2 && b == 2)
    {
        i_area = 8;
    }

    return i_area;
}
int compare(int *a, int t)
{
    for (int i = 0; i < 9; i++)
        if (a[i] == t)
            return 1;

    return 0;
}

void game_init()
{
    showInit();
    getInit();
    memset(T,-1,sizeof(T));
}

void radom(int tt)
{
    sleep(tt);
    // 0-2
    int a, b, c;
    while (1)
    {
        a = 0 + rand() % (3);
        b = 0 + rand() % (3);
        c = get_area(a, b);
        if (compare(T, c) == 0)
        {
            v = show(a, b, v);
            printf("随机数 :a=%d,b=%d\n", a, b);
            T[T_time] = c;
            printf("随机数区域=%d\n",c);
            T_time++;
            printf("第%d步\n", T_time);
            c = -1;
            break;
        }
    }
}

int get_point()
{
    GetDirection();
    int c;
    if (x_d < t_x[0] && y_d < t_y[0])
    {
        x_point = y_point = 0;
        c = 0;
        printf("(0,0)\n");
    } //(0,0)
    if (x_d > t_x[0] && x_d < t_x[1] && y_d < t_y[0])
    {
        x_point = 1;
        y_point = 0;
        c = 1;
        printf("(1,0)\n");
    } //(1,0)
    if (x_d > t_x[1] && y_d < t_y[0])
    {
        x_point = 2;
        y_point = 0;
        c = 2;
        printf("(2,0)\n");
    } //(2,0)
      //------------------------------------------------------------------//
    if (x_d < t_x[0] && y_d > t_y[0] && y_d < t_y[1])
    {
        x_point = 0;
        y_point = 1;
        c = 3;
        printf("(0,1)\n");
    } //(1,0)
    if (x_d > t_x[0] && x_d < t_x[1] && y_d > t_y[0] && y_d < t_y[1])
    {
        x_point = 1;
        y_point = 1;
        c = 4;
        printf("(1,1)\n");
    } //(1,1)
    if (x_d > t_x[1] && y_d > t_y[0] && y_d < t_y[1])
    {
        x_point = 2;
        y_point = 1;
        c = 5;
        printf("(2,1)\n");
    } //(1,2)
      //------------------------------------------------------------------//
    if (x_d < t_x[0] && y_d > t_y[1])
    {
        x_point = 0;
        y_point = 2;
        c = 6;
        printf("(0,2)\n");
    } //(2,0)
    if (x_d > t_x[0] && x_d < t_x[1] && y_d > t_y[1])
    {
        x_point = 1;
        y_point = 2;
        c = 7;
        printf("(1,2)\n");
    } //(2,1)
    if (x_d > t_x[1] && y_d > t_y[1])
    {
        x_point = 2;
        y_point = 2;
        c = 8;
        printf("(2,2)\n");
    } //(2,2)

    return c;
}

void people()
{
    //人为下棋
    while (1)
    {
        int r = get_point();
        if (compare(T, r) == 0)
        {
            v = show(x_point, y_point, v);
            printf("人下区域=%d\n", r);
            T[T_time] = r;
            T_time++; //步数+1
            printf("第%d步\n", T_time);
            break;
        }
        printf("已下过此区域\n");
    }
}



void game()
{
    game_init();
    show_bmp("./zj/game/game_bg.bmp", 0, 0);

    while (1)
    {
        for (int i = 0; i < 9; i++)
        {
            printf("T[%d]=%d\n",i,T[i]);
        }
        

        people();


        //判断是否为最后一步
        if (T_time == 9)
        {
            T_time = 0;
            v = 0;
            memset(T,-1,sizeof(T));
            return game();
        }

        //随机数下棋
        radom(1);
        

       


    }
}